//
//  ReplyItemCell.swift
//  maopu
//
//  Created by 黄海波 on 2017/4/20.
//  Copyright © 2017年 maopu. All rights reserved.
//

import UIKit

public enum VideoReplyCellType: Int {
    case reply        = 101
    case comment       = 102
}

class ReplyItemCell: UITableViewCell {
    @IBOutlet var headerImg:UIButton!
    @IBOutlet var userNameLab:UILabel!
    @IBOutlet var rankLab:UILabel! //等级
    @IBOutlet var sexImg:UIImageView!  //性别
    @IBOutlet var timeLab:UILabel!  //时间
    @IBOutlet var contentLB:UILabel!
    
    @IBOutlet var collectionView:UICollectionView!
    @IBOutlet var collectionView_height:NSLayoutConstraint!
    
    @IBOutlet var commentView:UIView!
    @IBOutlet var commentView_height:NSLayoutConstraint!
    @IBOutlet var firstCommentLab:UILabel!
    @IBOutlet var secondCommentLab:UILabel!
    @IBOutlet var moreCommentLab:UILabel!
    
//    @IBOutlet var lineView:UIView!
    @IBOutlet var lineView_minX:NSLayoutConstraint!
    
    @IBOutlet var upvoteView:UpvoteView!
    
    fileprivate var lineView:UIView!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        self.contentLB.font = UIFont.LightDefaultFont(15)
        self.contentLB.textColor = UIColor.m_textDeepColor()
        self.contentLB.numberOfLines = 0
        
        self.userNameLab.font = UIFont.RegularDefaultFont(14)
        self.userNameLab.textColor = UIColor.m_textDeepColor()
        self.userNameLab.text = "麦兜和鱼丸"
        
        self.headerImg.setTitle("", for: UIControlState())
        self.headerImg.setImage(UIImage.init(named: "test_header"), for: UIControlState())
        self.headerImg.layer.masksToBounds = true
        self.headerImg.layer.cornerRadius = 15
        
        self.rankLab.textAlignment = .center
        self.rankLab.contentMode = .center
        self.rankLab.layer.masksToBounds = true
        self.rankLab.layer.cornerRadius = 6
        self.rankLab.backgroundColor = UIColor.m_rankBgColor()
        self.rankLab.textColor = UIColor.m_rankTextColor()
        self.rankLab.font = UIFont.LightDefaultFont(10)
        self.rankLab.text = "  小猫猫  "
        
        self.timeLab.font = UIFont.RegularDefaultFont(11)
        self.timeLab.textColor = UIColor.m_textGrayColor()
        self.timeLab.text = "2楼"
        
        self.commentView.backgroundColor = UIColor.m_godReplyBgViewColor()
        
        self.lineView = UIView()
        self.lineView.backgroundColor = UIColor.m_lineColor()
        self.contentView.addSubview(self.lineView)
        self.lineView.snp.remakeConstraints { (make) in
            make.height.equalTo(1)
            make.left.equalTo(self).offset(60)
            make.right.equalTo(self)
            make.bottom.equalTo(self)
        }
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
    }
    
    func refreshCellData(indexPathRow:Int , cellType:VideoReplyCellType , hideUpvoteView:Bool = false){
        if hideUpvoteView{
            self.upvoteView.isHidden = true
        }else{
            self.upvoteView.isHidden = false
        }
        if cellType == .reply{
            self.refreshReplyCellData(indexPathRow: indexPathRow)
        }else{
            self.refreshCommentCellData(indexPathRow: indexPathRow)
        }
    }
    
    fileprivate func refreshReplyCellData(indexPathRow:Int){
        self.timeLab.text = "\(indexPathRow + 1)楼 昨天"
        self.firstCommentLab.text = "花花：你说的是什么？告白气球吗？哈哈，我喜欢哦 ,感觉很好听呢"
        self.secondCommentLab.text = "PoPo：[图片]我也不知道这是什么"
        
        self.contentView.backgroundColor = UIColor.white
        
        if indexPathRow == 0{
            self.contentLB.text = "我昨天还看到这个地方，哈哈，这里超级美，最喜欢咖啡厅里的角度，到时候是不是人手一杯啦"
            self.collectionView_height.constant = 60
            self.commentView_height.constant = 15
            
            self.firstCommentLab.isHidden = false
            self.secondCommentLab.isHidden = false
            self.moreCommentLab.isHidden = false
            
            
        }else if indexPathRow == 1{
            self.contentLB.text = "塞纳河畔，左岸的咖啡"
            self.collectionView_height.constant = 0
            self.commentView_height.constant = -93
            
            self.firstCommentLab.isHidden = true
            self.secondCommentLab.isHidden = true
            self.moreCommentLab.isHidden = true
            
        }else if indexPathRow == 2{
            self.contentLB.text = "塞纳河畔 左岸的咖啡 我手一杯 品尝你的美 留下唇印的嘴"
            self.collectionView_height.constant = 0
            self.commentView_height.constant = -15
            
            self.firstCommentLab.isHidden = false
            self.secondCommentLab.isHidden = false
            self.moreCommentLab.isHidden = true
        }else{
            
        }
        self.commentView.layoutIfNeeded()
        self.contentView.layoutIfNeeded()
    }
    
    fileprivate func refreshCommentCellData(indexPathRow:Int){
        self.lineView.backgroundColor = UIColor.RGB(r: 236, g: 236, b: 236)
        if indexPathRow == 0{
            self.lineView.snp.updateConstraints({ (make) in
                make.left.equalTo(self)
            })
        }else{
            self.lineView.snp.updateConstraints({ (make) in
                make.left.equalTo(self).offset(60)
            })
        }
        
        self.upvoteView.backgroundColor = UIColor.clear
        
        if indexPathRow == 0{
            self.contentView.backgroundColor = UIColor.white
            self.collectionView_height.constant = 60
        }else{
            self.collectionView_height.constant = -10
            self.contentView.backgroundColor = UIColor.m_viewBackgroundColor()
        }
        
        self.contentLB.text = "我昨天还看到这个地方，哈哈，这里超级美，最喜欢咖啡厅里的角度，到时候是不是人手一杯啦"
        self.collectionView.collectionViewLayout = self.initCollectionCellStyle(60, cellHeight: 60, minimumLineSpacing: 0)
        self.commentView.removeFromSuperview()
        self.contentView.snp.updateConstraints { (make) in
            make.width.equalTo(AppWidth)
            make.bottom.equalTo(self.collectionView).offset(10)
        }
        self.contentView.layoutIfNeeded()
    }
    
    //MARK: 设置CollectionCell的样式
    fileprivate func initCollectionCellStyle(_ cellWidth:CGFloat , cellHeight:CGFloat , minimumLineSpacing:CGFloat) -> UICollectionViewFlowLayout{
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.itemSize = CGSize(width: cellWidth, height: cellHeight)  // 设置cell大小
        flowLayout.scrollDirection = UICollectionViewScrollDirection.vertical  //设置垂直滚动
        flowLayout.minimumLineSpacing = minimumLineSpacing  //设置行间距
        flowLayout.minimumInteritemSpacing = 0
        return flowLayout
    }
    
}
